from typing import List, Union

class FrameStatus:
    """数据帧状态"""

    BUFFER_EMPTY: "FrameStatus"
    """接收缓存层为空"""

    BUFFER_INVALID: "FrameStatus"
    """接收缓存层无效"""

    BUFFER_TIMEOUT: "FrameStatus"
    """接收缓存层超时"""

    BUFFER_BROKEN: "FrameStatus"
    """接收缓存层损坏"""

    BUFFER_NOT_ENOUGH: "FrameStatus"
    """接收缓存层长度不够"""

    LENGTH_NOT_ENOUGH: "FrameStatus"
    """数据帧长度不够"""

    LENGTH_ERROR: "FrameStatus"
    """数据帧长度错误"""

    CRC_ERROR: "FrameStatus"
    """数据帧CRC校验错误"""

    INVALID: "FrameStatus"
    """数据帧无效"""

    VALID: "FrameStatus"
    """数据帧有效"""

class Frame:
    status: FrameStatus
    channel: int
    data: List[int]

    def __init__(self, status: FrameStatus, channel: int, data: List[int]) -> None:
        """initialize a new frame

        Args:
            status (FrameStatus): frame status
            channel (int): channel number
            data (List[int]): user data
        """
        ...

    def str_data(self) -> str:
        """Convert data into strings

        Returns:
            str: converted string
        """
        ...

class SimpleProtocolImpl:
    """Here is a simple protocol module for Python that supports a configurable communication protocol function. The protocol format is as follows: [SYNC_HEADER | FRAME_LEN | CHANNEL | DATA | CRC16].

    - SYNC_HEADER: Synchronization Header
    - FRAME_LEN: Frame Length, describing the length of the data for CHANNEL + DATA + CRC16
    - CHANNEL: Channel, used to distinguish different types of data
    - DATA: The data that the user needs to send
    - CRC16: When this feature is enabled, CRC16 is used for verification)"""

    def curr_timestamp_ms(self) -> int:
        """Get current time in milliseconds."""
        ...

    def buffer_is_empty(self) -> bool:
        """Check if the buffer is empty."""
        ...

    def buffer_flush(self) -> None:
        """Flush the buffer."""
        ...

    def push_back(self, data: Union[bytes, List[int]]) -> "SimpleProtocolImpl":
        """writes the received data into the cache

        Args:
            data (Union[bytes, List[int]]): Received data

        Returns:
            SimpleProtocolImpl: self
        """
        ...

    def parse(self) -> Frame:
        """Parsed frame of data

        Returns:
            Frame: Pase one frame
        """
        ...

    def parse_until_valid(self) -> Frame:
        """Parse until a valid frame is found or the buffer is empty

        Returns:
            Frame: Pase one frame

        """
        ...

    def parse_n(self, n: int) -> List[Frame]:
        """Parsed frame of n data

        Args:
            n (int): Hope to parse the number of frames

        Returns:
            List[Frame]: frames
        """
        ...

    def parse_all(self) -> List[Frame]:
        """Parse all the data in the buffer

        Returns:
            List[Frame]: all can be prased frames
        """
        ...

    def can_parsed(self, prve_status: FrameStatus) -> bool:
        """Based on the data returned from the previous frame, determine whether there is still a need to continue processing the data.

        Args: preve_status (FrameStatus): Previous frame status

        Returns:
            bool: Whether to continue processing
        """
        ...

    def pack(self, data: Union[bytes, List[int], str], channel: int) -> bytes:
        """Package the data required by the user into a qualified data frame

        Args:
            data (Union[bytes, List[int], str]): user input data
            channel (int): channel

        Returns:
            bytes: packed data
        """
        ...

class SimpleProtocol(SimpleProtocolImpl):
    """Here is a simple protocol module for Python that supports a configurable communication protocol function. The protocol format is as follows: [SYNC_HEADER | FRAME_LEN | CHANNEL | DATA | CRC16].

    - SYNC_HEADER: Synchronization Header
    - FRAME_LEN: Frame Length, describing the length of the data for CHANNEL + DATA + CRC16
    - CHANNEL: Channel, used to distinguish different types of data
    - DATA: The data that the user needs to send
    - CRC16: When this feature is enabled, CRC16 is used for verification)"""

    def __init__(
        self,
        sync_header: List[int],
        data_len_size: int,
        channel_size: int,
        en_crc: bool,
        max_frame_len: int,
        timeout_ms: int,
    ) -> None:
        """Initialize the protocol module.

        Args:
            sync_header (List[int]): 同步头
            data_len_size (int): 长度描述类型，最大长度为4字节
            channel_size (int): 通道描述类型，最大长度为2字节
            en_crc (bool):  是否启用CRC校验，如果开启，将对 “FRAME_LEN | CHANNEL | DATA ”数据进行校验
            max_frame_len (int):  最大包长度，包容长度“[SYNC_HEADER | FRAME_LEN | CHANNEL | DATA | CRC16]
            timeout_ms (int): 超时时间，单位为毫秒
        """
        ...

def crc16(buffer: Union[bytes, List[int]]) -> int: ...
